/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.view;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.biblioteca.bl.CategoriaBlLocal;
import mx.its.dsw2.biblioteca.modelo.Categoria;

/**
 *
 * @author Maq-8
 */
@ManagedBean
@SessionScoped
public class CategoryBean {

    @EJB
    private CategoriaBlLocal categoriaBl;
    private List<Categoria> lista;
    private Categoria category;
    private String accion;

    /**
     * Creates a new instance of CategoryBean
     */
    public CategoryBean() {
        category = new Categoria();
    }

    public List<Categoria> getLista() {
        lista = categoriaBl.getAll();
        return lista;
    }

    public Categoria getCategory() {
        return category;
    }

    public void setCategory(Categoria category) {
        this.category = category;
    }

    //Acciones
    public String procesarAccion() {
        if (accion.equalsIgnoreCase("Nuevo")) {
            categoriaBl.register(category);
        } else {
            if (accion.equalsIgnoreCase("Editar")) {
                categoriaBl.modify(category);
            } else {
                if (accion.equalsIgnoreCase("Eliminar")) {
                    categoriaBl.delete(category);
                }
            }
        }
        accion = "";
        return "CategoryList";
    }

    public String procesarLlamada() {
        String page = "";
        if (accion.equalsIgnoreCase("Editar") || accion.equalsIgnoreCase("Nuevo")) {
            page = "CategoryCreateEdit";
        } else {
            if (accion.equalsIgnoreCase("Eliminar")) {
                page = "CategoryList";
            }
        }
        return page;
    }

//Listeners
    public void prepararNuevo(ActionEvent evt) {
        accion = "Nuevo";
        category = new Categoria();
    }

    public void prepararEditar(ActionEvent evt) {
        accion = "Editar";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        category = lista.get(index);
    }

    public void prepararEliminar(ActionEvent evt) {
        accion = "Eliminar";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        category = lista.get(index);
    }
}
